/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.psc.controllers;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;

/**
 *
 * @author A7med
 */
@Named
@RequestScoped
public class FilesBean {

    @Resource(lookup = "PROJECTS_FOLDER")
    private Properties p;

    public void createDirectory(String dirName) {
        String base = p.getProperty("uri");
        File f = new File(base + "/" + dirName);
        System.out.println("project name="+f.getAbsolutePath());
        if (!f.exists()) {
          
            try { 
                
                if(f.mkdirs()){
                System.out.println("making dir");
                }
            } catch (Exception e) {
                System.out.println("exception:"+e.getMessage());
            }
        } else {
            System.out.println("file already exists");
        }

    }

    public InputStream getExcelStream(String projectName, String type) {
        String base = p.getProperty("uri");
        InputStream file = null;
        try {
            file = new FileInputStream(new File(base + "/" + projectName + "/" + type));
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return file;

    }

    public void handleFileUpload(FileUploadEvent event) {
        String base = p.getProperty("uri");
        try (OutputStream out = new FileOutputStream(new File(base + "/" + event.getFile().getFileName()));) {
            int read = 0;
            byte[] bytes = new byte[1024];
            InputStream in = event.getFile().getInputstream();
            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();

            System.out.println("New file created!");

        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void handleFileUpload(UploadedFile file, String dir) {
        String base = p.getProperty("uri");
        try (OutputStream out = new FileOutputStream(new File(base + "/" + dir + "/logo.png"));) {
            int read = 0;
            byte[] bytes = new byte[1024];
            try (InputStream in = file.getInputstream()) {
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
            }
            out.flush();
            out.close();

            System.out.println("New file created!");

        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void handleResUpload(UploadedFile file, String dir) {
        String base = p.getProperty("uri");
        try (OutputStream out = new FileOutputStream(new File(base + "/" + dir + "/" + file.getFileName()));) {
            int read = 0;
            byte[] bytes = new byte[1024];
            try (InputStream in = file.getInputstream()) {
                while ((read = in.read(bytes)) != -1) {
                    out.write(bytes, 0, read);
                }
            }
            out.flush();
            out.close();

            System.out.println("New file created!");

        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    StreamedContent getImage(String projectName) {
        try {
            String base = p.getProperty("uri");
            System.out.println("base=" + base + " and name=" + projectName);
            StreamedContent image = new DefaultStreamedContent(new FileInputStream(base + "/" + projectName + "/logo.png"));
            return image;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    StreamedContent getMedia(String projectName, String type,String section, String fileName,String contentType) {
        try {
            String base = p.getProperty("uri");
            System.out.println("base=" + base + " and name=" + projectName);
            StreamedContent image = new DefaultStreamedContent(new FileInputStream(base + "/" + projectName + "/" +section+ "/" + type + "/" + fileName), contentType,fileName);
            return image;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FilesBean.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
}
